!function (window) {
    /**
     * Created by Administrator on 2016/5/4 0004.
     */
    window.onload = function () {
        /*搜索区块的颜色变化*/
        search();
        /*轮播图*/
        banner();
        /*倒计时*/
        downTime();
    };
    function search() {
        var searchBox = document.querySelector(".ls_header_box");
        var bannerBox = document.querySelector(".ls_banner");
        var h = bannerBox.offsetHeight;

        //监听window的滚动事件
        window.onscroll = function () {
            var top = document.body.scrollTop;
            var op = 0;
            if (top < h) {
                op = top / h * 0.85;
            }else {
                op = 0.85;
            }
            searchBox.style.background = "rgba(110,110,110,"+ op +")";
        }
    };
    function banner() {
        var banner = document.querySelector(".ls_banner");
        var w = banner.offsetWidth;
        var imageBox = document.querySelector("ul:first-child");
        var pointBox = document.querySelector("ul:last-child");
        var points = pointBox.querySelectorAll("li");

        //添加过渡
        var addTransition = function () {
            imageBox.style.webkitTransition = "all 0.1s";
            imageBox.style.transition = "all 0.1s";
        }
        //移除过渡
        var removeTransition = function () {
            imageBox.style.webkitTransition = "none";
            imageBox.style.transition = "none";
        }
        //改变位置
        var setTransform = function (x) {
            imageBox.style.webkitTransform = "translateX("+ x +"px)";
            imageBox.style.transform = "translateX("+ x +"px)";
        }

        //让轮播图动起来
        var index = 1;
        var timer = setInterval(function () {
            index++;
            addTransition();
            setTransform(-index * w);
        }, 2000);

        //过渡结束事件
        lsj.transitionEnd(imageBox, function () {
            if(index >= 4) {
                index = 1;
                removeTransition();
                setTransform(-index * w);
            }else if (index <= 0) {
                index = 3;
                removeTransition();
                setTransform(-index * w);
            }
            setPoint();
        });
        function setPoint() {
            for (var i = 0; i < points.length; i++) {
                points[i].className = "";
            }
            points[index - 1].className = "now";
        }

        //设置数据
        var startX = 0;
        var endX = 0;
        var distance = 0;
        var isMove = false;
        imageBox.addEventListener("touchstart", function (e) {
            clearInterval(timer);
            startX = e.touches[0].clientX;
        });
        imageBox.addEventListener("touchmove", function (e) {
            isMove = true;
            endX = e.touches[0].clientX;
            distance = endX - startX;

            var currX = - index * w + distance;
            removeTransition();
            setTransform(currX);
        });
        imageBox.addEventListener("touchend", function (e) {
            if(isMove && Math.abs(distance) > w/3) {
                if (distance > 0) {
                    index--;
                }else {
                    index++;
                }
                addTransition();
                setTransform(-index * w);
            }else {
                addTransition();
                setTransform(-index * w);
            }
            clearInterval(timer);
            timer = setInterval(function () {
                index++;
                addTransition();
                setTransform(-index * w);
            }, 2000);
        });
    }

//秒杀时间
    function downTime() {
        var time = 5 * 60 * 60;
        var skTime = document.querySelector(".sk_time");
        var spans = skTime.querySelectorAll("span");
        var timer = setInterval(function () {
            if (time <= 0) {
                clearInterval(timer);
                return false;
            }
            time--;
            var h = Math.floor(time / 3600);
            var m = Math.floor(time % 3600 / 60);
            var s = time % 60;

            spans[0].innerHTML = Math.floor(h / 10);
            spans[1].innerHTML = h % 10;

            spans[3].innerHTML = Math.floor(m / 10);
            spans[4].innerHTML = m % 10;

            spans[6].innerHTML = Math.floor(s / 10);
            spans[7].innerHTML = s % 10;
        }, 1000);
    }
}(window)